package com.example.tool.util.zto;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.tool.zop.ZopClient;
import com.example.tool.zop.ZopPublicRequest;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.Arrays;

import static com.example.tool.zop.EncryptionType.MD5;


public class ZtoApiUtil {


    private static String appKey = "22f4fdef307235c60118f";
    private static String appSecret = "6bc97e03c9c19da6ef90d4e6ceef1405";
    private static String wtqUrl = "https://japi.zto.com/zto.merchant.waybill.track.query";
    private static String bdeaUrl = "https://japi.zto.com/zto.open.bindingEaccount";
    private static String sbUrl = "https://japi.zto.com/zto.merchant.waybill.track.subsrcibe";

/*    public static void main(String[] args) throws IOException {

        List<String> list = Arrays.asList(
                "78507584135047"

        );
        Set<String> set = new LinkedHashSet<>(list);
        List<String> listWithoutDuplicates = new ArrayList<>(set);
        for (String no: listWithoutDuplicates){

        System.out.println(queryLogistics(no));

        }


    }*/

    /**
     * 绑定电子面单
     *
     * @param eaccount      电子面单账号
     * @param siteCode      网点code
     * @param a0ddress      地址：省市区
     * @param addressDetail 详细地址
     * @param eaccountPwd   电子面单密码
     */
    public static void bindingEAccount(String eaccount, String siteCode, String a0ddress, String addressDetail, String eaccountPwd) {
        ZopClient client = new ZopClient(appKey, appSecret);
        ZopPublicRequest request = new ZopPublicRequest();
        JSONObject body = new JSONObject();
        body.put("eaccount", eaccount);
        body.put("siteCode", siteCode);
        body.put("address", a0ddress);
        body.put("addressDetail", addressDetail);
        body.put("eaccountPwd", eaccountPwd);
        request.setBody(new Gson().toJson(body));
        request.setUrl(bdeaUrl);
        request.setBase64(true);
        request.setEncryptionType(MD5);
        request.setTimestamp(null);
        String execute = null;
        try {
            execute = client.execute(request);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }

    /**
     * 物流轨迹查询
     *
     * @param waybillNo   运单号
     * @param mobilePhone 收寄人任一方电话号码后4位（手机或座机）。
     *                    通过电话号码鉴权时必填，鉴权方式可以电子面单账号或电话号码二选一。
     *                    选择电子面单账号鉴权时，该字段非必填；
     *                    选择电话号码鉴权时，可以不绑定下单电子面单账号。
     */
    public static JSONObject wayBillTrackQuery(String waybillNo, String mobilePhone) {
        ZopPublicRequest request = new ZopPublicRequest();
        request.setUrl(wtqUrl);
        JSONObject body = new JSONObject();
        body.put("billCode", waybillNo);
        body.put("mobilePhone", mobilePhone);
        request.setBody(new Gson().toJson(body));
        request.setBase64(true);
        request.setEncryptionType(MD5);
        request.setTimestamp(null);
        ZopClient client = new ZopClient(appKey, appSecret);
        String execute = null;
        try {
            execute = client.execute(request);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return JSONObject.parseObject(execute);
    }

    /**
     * 物流轨迹查询
     *
     * @param waybillNo 运单号
     */
    public static JSONObject queryLogistics(String waybillNo) {
        return wayBillTrackQuery(waybillNo, null);
    }


    /**
     * 物流轨迹查询
     *
     * @param waybillNos 运单号 ，隔开
     */
    public static JSONArray wayBillTracksQuery(String waybillNos) {
        JSONArray datas = new JSONArray();
        Arrays.stream(waybillNos.split(",")).distinct().forEach(waybillNo -> {
            datas.add(queryLogistics(waybillNo));
        });
        return datas;
    }


    public static JSONObject getOrderInfo(String waybillNo) {
        ZopPublicRequest request = new ZopPublicRequest();
        JSONObject requestBody = new JSONObject();
        requestBody.put("type", 1);
        requestBody.put("billCode", waybillNo);
        request.setBody(requestBody.toJSONString());
        request.setUrl("https://japi.zto.com/zto.open.getOrderInfo");
        try {
            return JSONObject.parseObject(new ZopClient(appKey, appSecret).execute(request));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static JSONObject sendSmsByBillCode(String waybillNo, String content, String smsSign) {
        ZopPublicRequest request = new ZopPublicRequest();
        JSONObject requestBody = new JSONObject();
        requestBody.put("billCode", waybillNo);
        requestBody.put("bizId", "bizId");
        requestBody.put("content", content);
        requestBody.put("smsSign", smsSign);
        request.setBody(requestBody.toJSONString());
        request.setUrl("https://japi.zto.com/zto.open.getOrderInfo");
        try {
            return JSONObject.parseObject(new ZopClient(appKey, appSecret).execute(request));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 订阅物流单号
     *
     * @param waybillNo 物流单号
     */
    public static void trackSubSubscribe(String waybillNo) {
        ZopClient client = new ZopClient(appKey, appSecret);
        ZopPublicRequest request = new ZopPublicRequest();
        JSONObject body = new JSONObject();
        body.put("billCode", waybillNo);
        request.setBody(new Gson().toJson(body));
        request.setUrl(sbUrl);
        request.setBase64(true);
        request.setEncryptionType(MD5);
        request.setTimestamp(null);
        String execute = null;
        try {
            execute = client.execute(request);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
//        queryLogistics("78921244083001");
//        System.out.println(queryLogistics("78898098644849"));
//        System.out.println(wayBillTrackQuery("78898098644849","9729"));
//        System.out.println(queryLogistics("78510198423315"));
        System.out.println(getOrderInfo("78510198423315"));
//        System.out.println(wayBillTrackQuery("78521433706560","5737"));
    }


}
